<?php

namespace App\Http\Requests\finance;

use App\Http\ValidateResult;
use Illuminate\Support\Facades\Validator;

trait TmpRequest
{
    public static function add_check(array $params): ValidateResult {
        $validator = Validator::make($params, [
            'name' => 'required',
            'detail' => 'required|array',

        ], [
            'name.required' => '请填写模板名称',
            'detail.required' => '请填写费用明细',
            'detail.array' => '费用明细格式不正确',
        ]);

        if ($validator->fails()) {
            return ValidateResult::error($validator->errors()->first());
        }

        $detail = $params['detail'];

        foreach ($detail as $item){
            $validator = Validator::make($item, [
                'name' => 'required',
                'agent_id' => 'required',
                'number' => 'required',
                'unit' => 'required',
                'price' => 'required',
                'currency_id' => 'required',
            ], [
                'name.required' => '请填写费用项目',
                'agent_id.required' => '请选择供应商',
                'number.required' => '请填写数量',
                'unit.required' => '请填写单位',
                'price.required' => '请填写单价',
                'currency_id.required' => '请选择币种',
            ]);

            if ($validator->fails()) {
                return ValidateResult::error($validator->errors()->first());
            }
        }


        return ValidateResult::correct();
    }

    public static function add_check2(array $params): ValidateResult {
        $validator = Validator::make($params, [
            'name' => 'required',
            'detail' => 'required|array',

        ], [
            'name.required' => '请填写模板名称',
            'detail.required' => '请填写费用明细',
            'detail.array' => '费用明细格式不正确',
        ]);

        if ($validator->fails()) {
            return ValidateResult::error($validator->errors()->first());
        }

        $detail = $params['detail'];

        foreach ($detail as $item){
            $validator = Validator::make($item, [
                'name' => 'required',
//                'agent_id' => 'required',
                'number' => 'required',
                'unit' => 'required',
                'price' => 'required',
                'currency_id' => 'required',
            ], [
                'name.required' => '请填写费用项目',
//                'agent_id.required' => '请选择供应商',
                'number.required' => '请填写数量',
                'unit.required' => '请填写单位',
                'price.required' => '请填写单价',
                'currency_id.required' => '请选择币种',
            ]);

            if ($validator->fails()) {
                return ValidateResult::error($validator->errors()->first());
            }
        }


        return ValidateResult::correct();
    }

}
